package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.EnjoyUserService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.EnjoyBorrowDao;
import com.qdlc.p2p.dal.dao.EnjoyUserDao;
import com.qdlc.p2p.dal.dto.EnjoyUser;
import com.qdlc.p2p.dal.model.EnjoyUserModel;

/**
 * Created by D on 2016/1/21.
 */
@Service("enjoyUserService")
public class EnjoyUserServiceImpl implements EnjoyUserService {

    @Resource
    private EnjoyUserDao enjoyUserDao;
    @Resource
    private EnjoyBorrowDao enjoyBorrowDao;

    @Override
    public void giveGoods(EnjoyUser enjoyUser) {
        //增加商品记录
        enjoyUserDao.save(enjoyUser);
        //已发放商品数增加
        enjoyBorrowDao.giveGoods(enjoyUser.getAmount(),enjoyUser.getEnjoyBorrow().getId());
    }

    @Override
    public PageDataList<EnjoyUserModel> findByModel(EnjoyUserModel model) {
        QueryParam param = QueryParam.getInstance();
        if(!StringUtil.isBlank(model.getSearchName())){//模糊查询条件
            SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE , model.getSearchName().trim());
            param.addOrFilter(orFilter1);
        } else { //精确查询条件
            if (!StringUtil.isBlank(model.getUserName())) {
                param.addParam("user.userName", Operators.LIKE , model.getUserName().trim());
            }
            if (!StringUtil.isBlank(model.getBorrowName())) {
                param.addParam("enjoyBorrow.borrow.name", Operators.LIKE , model.getBorrowName().trim());
            }
            if (!StringUtil.isBlank(model.getGoodsName())) {
                param.addParam("enjoyBorrow.enjoyGoods.name", Operators.LIKE , model.getGoodsName().trim());
            }
            if (model.getStatus() != 0) {
                param.addParam("status", model.getStatus());
                }
            if (StringUtil.isNotBlank(model.getStartTime())) {
                Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
                param.addParam("addTime", Operators.GTE, start);
            }
            Date nowdate = DateUtil.getDate((System.currentTimeMillis() / 1000) + "");
            if (model.getTime() == 7) {
                param.addParam("addTime", Operators.GTE, DateUtil.rollDay(nowdate, -7));
                param.addParam("addTime", Operators.LTE, nowdate);
            } else if ((model.getTime() > 0) && (model.getTime() < 4)) {
                param.addParam("addTime", Operators.GTE, DateUtil.rollMon(nowdate, -model.getTime()));
                param.addParam("addTime", Operators.LTE, nowdate);
            }
            if (StringUtil.isNotBlank(model.getEndTime())) {
                Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
                param.addParam("addTime", Operators.LTE, end);
            }
            }
        // 用户账户管理页面-我的趣享商品
        if(model.getUser()!=null){
            param.addParam("user.userId", model.getUser().getUserId());
        }

        param.addPage(model.getPage(), model.getRows());
        param.addOrder(OrderType.DESC, "id");
        PageDataList<EnjoyUser> pageDataList = enjoyUserDao.findPageList(param);
        PageDataList<EnjoyUserModel> pageDataList_ = new PageDataList<EnjoyUserModel>();
        List<EnjoyUserModel> list = new ArrayList<EnjoyUserModel>();
        pageDataList_.setPage(pageDataList.getPage());
        if (pageDataList.getList().size() > 0) {
            for (EnjoyUser enjoyUser : pageDataList.getList()) {
                EnjoyUserModel enjoyUserModel = EnjoyUserModel.instance(enjoyUser);
                enjoyUserModel.setUserName(enjoyUser.getUser().getUserName());
                enjoyUserModel.setUser(enjoyUser.getUser());
                enjoyUserModel.setGoodsName(enjoyUser.getEnjoyBorrow().getEnjoyGoods().getName());
                enjoyUserModel.setBorrowName(enjoyUser.getEnjoyBorrow().getBorrow().getName());
                enjoyUserModel.setUuid(enjoyUser.getEnjoyBorrow().getBorrow().getUuid());
                list.add(enjoyUserModel);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }

    @Override
    public void updateStatus(long borrowId,int status) {
        long enjoyBorrowId=enjoyBorrowDao.findByProperty("borrow.id",borrowId).get(0).getId();
        enjoyUserDao.updateStatus(enjoyBorrowId,status,EnjoyUser.WAIT_GIVE);
    }
}
